/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: message_builder.h
 *
 * Purpose: builder and parser implmenttation for fdbus
 *
 * Developer:
 *   wen.gu , 2021-05-10
 *
 * TODO:
 *
 ***************************************************************************/

/******************************************************************************
 **    INCLUDES
 ******************************************************************************/
#ifndef __MESSAGE_BUILDER_H__
#define __MESSAGE_BUILDER_H__

#include "common_base/IFdbMsgBuilder.h"
#include "common_base/fdbus.h"

#include "message.h"
/******************************************************************************
 **    MACROS
 ******************************************************************************/


/******************************************************************************
 **    TYPE DEFINITIONS
 ******************************************************************************/

#define MAKE_MY_MESSAGE() std::make_shared<MyMessage>()

/******************************************************************************
 **    CLASSES/FUNCTIONS DEFINITIONS
 ******************************************************************************/
class MyMessage: public Message
{
public:
    MyMessage(MessageType type)
    :Message(type)
    {
        //todo something
    }
    MyMessage()
    :Message(MessageType::kEvent) /** just as a default type*/
    {
        //todo something        
    }
public:
    void set_msg_ref(CBaseJob::Ptr& msg_ref) {msg_ref_ = msg_ref;}
    CBaseJob::Ptr msg_ref() const { return msg_ref_;}
private:
    CBaseJob::Ptr msg_ref_ = nullptr;
};

using MyMessagePtr = std::shared_ptr<MyMessage>;

class MessageBuilder: public IFdbMsgBuilder
{
public:
    MessageBuilder(MessagePtr msg);
    virtual ~MessageBuilder();
public:
    /**
     * the protocol of message data:
     * |session id(4 bytes)| message id(4 bytes)| message type(1 byte)|error_code(1 byte)| payload size(4 bytes)| payload data (variable length)| 
     */

    int32_t build() override;
    bool toBuffer(uint8_t *buffer, int32_t size) override;
private:
    MessagePtr msg_;
};


class MessageParser: public IFdbMsgParser
{
public:
    MessageParser(MessagePtr msg);
    virtual ~MessageParser();

public:
    bool parse(const uint8_t *buffer, int32_t size) override;

public:
    MessagePtr get_message() const { return msg_;}

private:
    MessagePtr msg_;
};



#endif /** !__MESSAGE_BUILDER_H__ */

